home *** CD-ROM | disk | FTP | other *** search
- #include <osbind.h>
- #include <mintbind.h>
- #include <ctype.h>
- #include <string.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include "global.h"
-
- #define SYSVAR_bootdev (*((short *)0x446UL))
-
-
- unsigned char *file_data = 0;
- Var *vars = 0;
- static char file_name[160] = "A:\\STIK_CFG\\DEFAULT.CFG";
- static char stikdir_dat[] = "A:\\STIK_DIR.DAT";
- static char default_config[] = "ALLOCMEM = 50000\n";
-
-
- static int get_bootdrive(void)
- {
- return (int)SYSVAR_bootdev;
- }
-
- static int find_config_file(void)
- {
- register long l;
- register int n, fd, boot;
- register char *s;
-
- boot = Supexec(get_bootdrive);
- file_name[0] += boot;
- stikdir_dat[0] += boot;
-
- if ((l = Fopen(stikdir_dat, 0)) < 0)
- return 1;
- fd = (int)(short)l;
- if ((n = Fread(fd, sizeof(file_name), file_name)) < 0) {
- Fclose(fd);
- Cconws("Error reading STIK_DIR.DAT; using default path\r\n");
- return 1;
- }
- if (n >= sizeof(file_name))
- n = sizeof(file_name) - 1;
- if (!(s = strchr(file_name, '\n')))
- s = file_name + n;
- if (s[-1] == '\r')
- s--;
- *s = 0;
- /* Incompatibility: The STiK docs imply that you can change the
- config file name via STIK_DIR.DAT, but then say to include only the
- new path. I'm gonna handle it this way: if the line read from
- STIK_DIR.DAT has a trailing '/' or '\', assume it's a path and
- append "DEFAULT.CFG"; otherwise, assume it's the whole file name
- and return it untouched. */
- if (s[-1] == '\\' || s[-1] == '/')
- strcpy(s, "DEFAULT.CFG");
- return 1;
- }
-
- #define NEXT_LINE() {while (*s) s++; while (!*s) s++;}
-
- int load_config_file()
- {
- register unsigned char *s;
- unsigned long filesize;
- long l;
- int n, fd;
- struct stat S;
-
- if (!find_config_file()) { /* Okay, so I'm paranoid... ;) */
- Cconws("Unable to find config file\r\n");
- return -1;
- }
- if (Fxattr(0, file_name, &S)) {
- Cconws("Config file ");
- Cconws(file_name);
- Cconws(" does not exist; using built-in defaults...\r\n");
-
- file_data = (unsigned char *)Mxalloc(sizeof(default_config), 3);
- if (!file_data) {
- Cconws("Cannot get memory for default config file\r\n");
- return -1;
- }
- strcpy((char *)file_data, default_config);
- } else {
- filesize = S.st_size;
- Cconws("Reading config file ");
- Cconws(file_name);
- Cconws("...\r\n");
-
- file_data = (unsigned char *)Mxalloc(filesize + 2, 3);
- if (!file_data) {
- Cconws("Cannot get memory to read config file\r\n");
- return -1;
- }
-
- if ((l = Fopen(file_name, 0)) < 0) {
- Cconws("Error opening config file ");
- Cconws(file_name);
- Cconws("\r\n");
- return -1;
- }
- fd = (int)(short)l;
-
- s = file_data;
- while ((n = Fread(fd, filesize, s)) > 0)
- s += n;
- if (n < 0) {
- Cconws("Error reading config file ");
- Cconws(file_name);
- Cconws("\r\n");
- return -1;
- }
- Fclose(fd);
- *s = 0;
- }
-
- for (s = file_data, l = 0; *s; s++) {
- if (*s == '\r')
- *s = 0;
- if (*s == '\n')
- *s = 0, l++;
- }
- /* make sure last line gets counted */
- if (s[-1] != '\0')
- l++;
-
- vars = (Var *)Mxalloc((l + 1) * sizeof(Var), 3);
- if (!vars) {
- Cconws("Cannot get memory for config vars\r\n");
- return -1;
- }
- s = file_data;
- n = 0;
- while (s < file_data + filesize && n < l) {
- int has_equals = 0;
-
- while (*s && isspace(*s))
- s++;
- if (!*s || *s == '#') {
- NEXT_LINE();
- continue;
- }
- vars[n].name = (char *)s;
- while (*s && !isspace(*s) && *s != '=')
- s++;
- if (!*s) {
- vars[n++].value = "1";
- continue;
- }
- if (*s == '=')
- has_equals = 1;
- *s++ = 0;
- while (*s && isspace(*s))
- s++;
- if (!has_equals && *s == '=') {
- has_equals = 1;
- s++;
- while (*s && isspace(*s))
- s++;
- }
- if (*s)
- vars[n].value = (char *)s;
- else if (has_equals)
- vars[n].value = "0";
- else
- vars[n].value = "1";
- n++;
- NEXT_LINE();
- }
- vars[n].name = vars[n].value = 0;
- return 0;
- }
-
- char* do_getvstr(char *var)
- {
- register int n;
-
- for (n = 0; vars[n].name; n++) {
- if (stricmp(var, vars[n].name) == 0) {
- #ifdef DEBUG
- debug("sssss", "getvstr(\"", var, "\") returns \"", vars[n].value, "\"");
- #endif
- return vars[n].value;
- }
- }
- #ifdef DEBUG
- debug("sss", "getvstr(\"", var, "\") returns \"0\"");
- #endif
- return "0";
- }
-